<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_phantrang{
        
        var   $previous_btn = true;
        var   $next_btn = true;
        var   $first_btn = true;
        var   $last_btn = true;
        
        public function __construct($params = array())
	{
		if (count($params) > 0)
		{
			$this->initialize($params);
		}

	}
        function initialize($params = array())
	{
		if (count($params) > 0)
		{
			foreach ($params as $key => $val)
			{
				if (isset($this->$key))
				{
					$this->$key = $val;
				}
			}
		}
	}
        function pagination($page,$total,$url,$trang=1)
        {
            $CI =& get_instance();
            $CI->load->helper('url');
            //kiem tra

            $count=$total;
            $tongtrang=ceil($total/$page);
            $num="";

            if($count!=0)
            {
                    if ($trang >= 7) {
                            $start_loop = $trang - 4;
                            if ($tongtrang > $trang + 4)
                                    $end_loop = $trang + 4;
                            else if ($trang <= $tongtrang && $trang > $tongtrang - 6) {
                                    $start_loop = $tongtrang - 6;
                                    $end_loop = $tongtrang;
                            } else {
                                    $end_loop = $tongtrang;
                            }
                    } else {
                            $start_loop = 1;
                            if ($tongtrang > 7)
                                    $end_loop = 7;
                            else
                                    $end_loop = $tongtrang;
                    }
            }


        // FOR ENABLING THE FIRST BUTTON
            if ($first_btn && $trang > 1) {
                    $dau = "<li  class='on'><a href='".site_url($url)."'>Đầu</a></li>";
            } else if ($first_btn) {
                    $dau= "<li  class='off'> <a> Đầu</a></li>";
            }

            // FOR ENABLING THE PREVIOUS BUTTON
            if ($previous_btn && $trang > 1) {
                    $tam=$trang-1;
                    $lui = "<li class='on'><a href='".site_url($url. $tam)."'> &laquo;</a></li>";
            } else if ($previous_btn) {
                    $lui = "<li class='off'><a>&laquo;</a></li>";
            }


            if ($next_btn && $trang < $tongtrang) {
                    $tam2=$trang+1;
                    $toi = "<li class='on'><a href='".site_url($url. $tam2)."'> &raquo; </a></li>";
            } else if ($next_btn) {
                    $toi = "<li class='off'> <a>&raquo; </a></li>";
            }

            // TO ENABLE THE END BUTTON
            if ($last_btn && $trang < $tongtrang) {
                    $cuoi= "<li  class='on'><a href='".site_url($url.$tongtrang)."'> Cuối </a></li>";
            } else if ($last_btn) {
                    $cuoi = "<li class='off'><a>Cuối</a></li>";
            }
            if($count>0)
            {
                    for($i=$start_loop;$i<=$end_loop;$i++)
                    {
                            if($i==$trang)
                            $num.="<li class='p'><a>$i</a></li>";
                            else
                            $num.="<li class='on'><a href='".site_url($url . $i)."' title=''>$i</a></li>";
                    }
            }
            if($count>0&&$tongtrang>1)
            $link=" 
            <ul class='pagination'>

                    ".$dau.$lui.$num.$toi.$cuoi."

            </ul>
                    ";
            else
            $link='';

            return $link;
    }
/*
    function paging_ajax($page,$total,$url,$trang=1)
            {
                    $previous_btn = true;
                    $next_btn = true;
                    $first_btn = true;
                    $last_btn = true;
                    //kiem tra


                    $count=$total;
                    $tongtrang=ceil($total/$page);
                    $num="";

                    if($count!=0)
                    {
                            if ($trang >= 7) {
                                    $start_loop = $trang - 4;
                                    if ($tongtrang > $trang + 4)
                                            $end_loop = $trang + 4;
                                    else if ($trang <= $tongtrang && $trang > $tongtrang - 6) {
                                            $start_loop = $tongtrang - 6;
                                            $end_loop = $tongtrang;
                                    } else {
                                            $end_loop = $tongtrang;
                                    }
                            } else {
                                    $start_loop = 1;
                                    if ($tongtrang > 7)
                                            $end_loop = 7;
                                    else
                                            $end_loop = $tongtrang;
                            }
                    }


                    // FOR ENABLING THE FIRST BUTTON
                    if ($first_btn && $trang > 1) {
                            $dau = "<li  class='on'><a href='".$url."#trang-". 1 ."'>Đầu</a></li>";
                    } else if ($first_btn) {
                            $dau= "<li  class='off'> <a> Đầu</a></li>";
                    }

                    // FOR ENABLING THE PREVIOUS BUTTON
                    if ($previous_btn && $trang > 1) {
                            $tam=$trang-1;
                            $lui = "<li class='on'><a rel='".$tam."' href='".$url."#trang-".$tam."'> Lùi</a></li>";
                    } else if ($previous_btn) {
                            $lui = "<li class='off'><a>Lùi</a></li>";
                    }


                    if ($next_btn && $trang < $tongtrang) {
                            $tam2=$trang+1;
                            $toi = "<li class='on'><a rel='".$tam2."' href='".$url."#trang-".$tam2."'> Tới </a></li>";
                    } else if ($next_btn) {
                            $toi = "<li class='off'> <a>Tới </a></li>";
                    }

                    // TO ENABLE THE END BUTTON
                    if ($last_btn && $trang < $tongtrang) {
                            $cuoi= "<li  class='on'><a rel='".$tongtrang."' href='".$url."#trang-".$tongtrang."'> Cuối </a></li>";
                    } else if ($last_btn) {
                            $cuoi = "<li class='off'><a>Cuối</a></li>";
                    }
                    if($count>0)
                    {
                            for($i=$start_loop;$i<=$end_loop;$i++)
                            {
                                    if($i==$trang)
                                    $num.="<li class='p'><a>$i</a></li>";
                                    else
                                    $num.="<li class='on'><a rel='".$i."'  href='".$url."#trang-".$i."' title=''>$i</a></li>";
                            }
                    }
                    if($count>0&&$tongtrang>1)
                    $link=" 
                    <ul class='pagination'>

                            ".$dau.$lui.$num.$toi.$cuoi."

                    </ul>
                            ";
                    else
                    $link='';

                    return $link;

    }*/
}
  
?>